package com.wechat.wechat.converter;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wechat.wechat.dto.OrderDTO;
import com.wechat.wechat.entity.OrderDetail;
import com.wechat.wechat.enums.ResultEnum;
import com.wechat.wechat.exception.SellException;
import com.wechat.wechat.form.OrderForm;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.criterion.Order;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * 将OrderForm转换为OrderDTO对象
 * @author NN
 * @2020/2/21 14:57
 */
@Slf4j
public class OrderForm2OrderDTOConverter {
    public static OrderDTO convert(OrderForm orderForm){
        Gson gson= new Gson();
        OrderDTO orderDTO=new OrderDTO();
        orderDTO.setBuyerAddress(orderForm.getAddress());
        orderDTO.setBuyerName(orderForm.getName());
        orderDTO.setBuyerPhone(orderForm.getPhone());
        orderDTO.setBuyerOpenid(orderForm.getOpenid());
        List<OrderDetail> orderDetailList = new ArrayList<>();
        try {
            orderDetailList = gson.fromJson(orderForm.getItems(),
                    new TypeToken<List<OrderDetail>>(){}.getType());
        }catch (Exception e)
        {
            log.error("【对象转换错误】错误,string={}",orderForm.getItems());
            throw new SellException(ResultEnum.PARAM_ERROR);
        }
        orderDTO.setOrderDetailList(orderDetailList);
        return orderDTO;
    }

}
